<?php
namespace Home\Controller;
header("Content-type: text/html; charset=utf-8");
use Think\Controller;
ini_set("memory_limit","40000M");
ini_set('max_execution_time', '0');
//
class HardController extends Controller
{

    /**
     * 初始化链
     *
     * @descript 如果未登录跳转至登录页面
     */
	function _initialize(){
//	   if (!session('?user')) {
//            $this->redirect('/Home/Login/login');
//       }

    }
    /**
     * 首页的数据
     */
    /**
     * 首页的数据
     */
    public function index(){
        if(strstr($_SESSION['aut'],'1')){
            $_SESSION['index'] = '1';
        } else{
            $_SESSION['index'] = '-1';
        }
        $this->display();
    }

    public function add(){
        $pub = M('publish');
        $data['sys']=$_POST['sys'];
        $data['version']=$_POST['version'];
        $data['doc']=$_POST['doc'];
        $data['size']=$_POST['size'];
        $data['sysinfo']=$_POST['sysinfo'];
        $data['hard_updtime']="";
        $data['hard_upgtime']="";
        $data['createtime']=time();
        $data['status']=1;


        $data['url']=$_POST['url'];


        $str=$_POST['doc'];
        $arr=explode("\n",$str);
        $str1=nl2br($str);//回车换成换行

        $data['doc']=$str1;
// 		echo $str1;


        $res = $pub->add($data);
        if($res){
            $res =$pub->where('status =1')->order('id desc')->find();
            $this->assign(res,$res);


            $black =M("whitetest")->where('type=1')->find();
            $this->assign(black,$black['name']);
            $this->display('white');

        }else{
            $this->error("数据添加失败！");
        }
    }

    public function upload(){
        $upload = M('publish');
        session_start();
        $data['sys'] = '3';
        $data['version'] = $_POST['version'];
        $data['size'] = $_POST['size'];
        $data['doc'] = $_POST['doc'];
        $data['sysinfo'] = $_POST['sysinfo'];
        $data['createtime'] = time();
        $data['status'] = 1;
//        echo '文件名:' . $_FILES['file']['name'] . '</br>';
//        echo '文件大小:' . $_FILES['file']['size'] . 'KB</br>';
//        echo '文件临时存储位置:' . $_FILES['file']['tmp_name'] . '</br>';
//        echo '文件转存位置:' .  __DIR__ . "/../../../crontabscript/examples/" . $_FILES['file']['name']. '</br>';
        $_SESSION['filename'] = $_FILES['file']['name'];
        $file_path = __DIR__ . '/../../../crontabscript/examples/' . $_FILES['file']['name'];
        if(is_uploaded_file($_FILES['file']['tmp_name'])){
            if (move_uploaded_file( $_FILES['file']['tmp_name'] , $file_path )){
                $file_contents = file_get_contents($file_path);
                $_SESSION['filecontent'] = $file_contents;
//                echo '文件转存内容:' . $file_contents . '</br>';
                include_once(__DIR__ ."/../../../crontabscript/examples/galaxy-fds-hard.php");
                $data['url'] = $_SESSION['download'];
                $res = $upload->add($data);
                echo "<p>上传成功</p>";
                echo $data['url'];
                unlink(__DIR__ . '/../../../crontabscript/examples/' . $_FILES['file']['name']);
                $this->display('index');
            }
            else{
                echo '文件转存失败';
            }
        }   else{
            echo '文件传输出错，非POST模式';
        }
        $this->display;


    }

    public function confirm(){
        if(strstr($_SESSION['aut'],'3')){
            $_SESSION['confirm'] = '1';
        } else {
            $_SESSION['confirm'] = '-1';
        }
        $pub = M("publish");
        $page = new \Think\Page($pub->where('status = 2 AND sys = 3')->count(),10);
        $data =$pub
            ->where('status = 2 AND sys = 3')
            ->order('createtime DESC')
            ->limit($page->firstRow. ',' .$page->listRows)
            ->select();
        $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $this->assign('data',$data);
        $this->assign('_page',$page->show());
        $this->display();
    }

    public function confirm_upload(){
        $pub = M("publish");
        $data['status'] = '3';
        $data['createtime'] = time();
        $res = $pub->where('id='.$_POST['id'])->save($data);
        if ($res){
            $data['status'] = '1';
        } else{
            $data['status'] = '-1';
        }
        $this->ajaxReturn($data);
    }

    public function confirm_shelf(){
        $pub = M("publish");
        $data['status']=1;
        $res =$pub->where('id='.$_POST['id'])->save($data);
        if($res){
            $data['status']=1;
        }else{
            $data['status']=-1;
        }
        $this->ajaxReturn($data);
    }

    public function pubshow(){
        if(strstr($_SESSION['aut'],'1')){
            $_SESSION['pubshow'] = '1';
        } else {
            $_SESSION['pubshow'] = '-1';
        }
        $pub = M("publish");
        $page = new \Think\Page($pub->where('status = 3 AND sys = 3')->count(),10);
        $data = $pub
            ->where('status = 3 AND sys = 3')
            ->order('createtime DESC')
            ->limit($page->firstRow. ',' .$page->listRows)
            ->select();
        $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $this->assign('data',$data);
        $this->assign('_page',$page->show());
        $this->display();
    }



    public function release(){
        $pub = M("publish");
        $where['status']=3;
        $where['id']=$_POST['id'];
        $data =$pub->save($where);
        if($data){
            $this->success('发布成功',U('pubshow'));
        }else{
            $this->error('发布失败');
        }

    }

    public function remove(){
        $res =M('publish')->where('id='.$_POST['id'])->delete();
        if($res){
            $data['status']=1;
        }else{
            $data['status']=-1;
        }
        $this->ajaxReturn($data);
    }

    public function shelf(){
        $where['status']=2;
        $res =M('publish')->where('id='.$_POST['id'])->save($where);
        if($res){
            $data['status']=1;
        }else{
            $data['status']=-1;
        }
        $this->ajaxReturn($data);
    }



    public function logout(){
        unset ($_SESSION['user']);
    }


    public function white(){
        if(strstr($_SESSION['aut'],'2')){
            $_SESSION['white'] = '1';
        } else {
            $_SESSION['white'] = '-1';
        }
        $conn = M('publish');
        $page = new \Think\Page($conn->where('status = 1 AND sys = 3')->count(),10);
        $data = $conn
            ->where('status = 1 AND sys = 3')
            ->order('createtime DESC')
            ->limit($page->firstRow. ',' .$page->listRows)
            ->select();
        $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $this->assign('data',$data);
        $this->assign('_page',$page->show());
        $this->display();
    }

    public function white_upload(){
        $upload = M('publish');
        $data['doc'] = $_POST['doc'];
        $data['sysinfo'] = $_POST['sysinfo'];
        $data['createtime'] = time();
        $data['status'] = 2;
        $res = $upload->where('id ='.$_POST['id'])->save($data);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        $this->ajaxReturn($data);
    }

    public function whitelist(){
        if(strstr($_SESSION['aut'],'9')){
            $_SESSION['whitelist'] = '1';
        } else {
            $_SESSION['whitelist'] = '-1';
        }
        $conn=M('whitelist');
        if(!$conn){
            echo "连接失败";
        }
        $page = new \Think\Page($conn->where('type = 2')->count(),10);
        $ls =$conn
            ->where('type = 2')
            ->order('time DESC')
            ->limit($page->firstRow. ',' .$page->listRows)
            ->select();
        $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $this->assign('data',$ls);
        $this->assign('_page',$page->show());
        $this->display();
    }

    public  function white_add(){
        $conn=M('whitelist');
        if(!$conn){
            echo "连接失败";
        }
        $data['name'] = $_POST['name'];
        $data['type'] = $_POST['type'];
        $data['serial'] = $_POST['serial'];
        $data['model'] = $_POST['model'];
        $data['person'] = $_POST['person'];
        $data['time'] = time();
        $res = $conn->add($data);
        if($res){
            $data['status']=1;
        }else{
            $data['status']=-1;
        }
        $this->ajaxReturn($data);
    }






    public  function white_save(){
        $conn=M('whitelist');
        if(!$conn){
            echo "连接失败";
        }
        $data['name'] = $_POST['name'];
        $data['type'] = $_POST['type'];
        $data['serial'] = $_POST['serial'];
        $data['model'] = $_POST['model'];
        $data['person'] = $_POST['person'];
        $data['time'] = time();
        $res = $conn->where('id='.$_POST['id'])->save($data);
        if($res){
            $data['status']=1;
        }else{
            $data['status']=-1;
        }
        $this->ajaxReturn($data);
    }


    public function white_del(){
        $res =M('whitelist')->where('id='.$_POST['id'])->delete();
        if($res){
            $data['status']=1;
        }else{
            $data['status']=-1;
        }
        $this->ajaxReturn($data);
    }


}